<?php

namespace app\admin\controller\attachment;

use app\admin\controller\attachment\Search;
use app\admin\controller\Base;
use app\admin\logic\Uploader;
use app\admin\model\Attachment as AttachmentModel;
use app\BaseController;
use brand\Brand;
use brand\logic\BrandHelper;
use brand\page\Form;
use surface\helper\Builder;
use surface\helper\Read;
use surface\table\Table;
use think\facade\View;
use app\admin\logic\Attachment as AttachmentLogic;

class Attachment extends Base
{

    use Read;

    public function indexTable()
    {
        return $this->createTable(new \app\admin\controller\attachment\Table());
    }


    public function delete_file($id)
    {
        if(AttachmentLogic::delete($id)){
            return _success("删除成功");
        }
        return _error("删除失败");
    }

    private function builderSearchAtt()
    {

        $search = Builder::form(new Search());;
        $search->execute();
        $search->search(true);
        $search->options([
            'props' => [
                'inline' => true,
            ],
            'submitBtn' => [
                'props' => [
                    'prop' => [
                        'icon' => 'el-icon-search',
                    ],
                ]
            ]
        ]);

        return $search;
    }



    public function manage()
    {

        $min = (int)(AttachmentModel::min('size') / 1000);// KB
        $max = (int)(AttachmentModel::max('size') / 1000) + 1;// KB
        $columns['name'] =  Brand::Builder('标题')->build();
        $columns['suffix'] =  Brand::Builder('后缀')->method_select()->form_options(BrandHelper::formatOptions(AttachmentModel::$typeLabels))->build();
        $columns['size'] =  Brand::Builder('尺寸(KB)')->method_slider()->form_props(['min' => $min, 'max' => $max, 'range' =>true])->build();

        $search = Form::$SurfaceForm;

        $search->options([
            'props' => [
                'inline' => true
            ],
            'submitBtn' => [
                'props' => [
                    'prop' => [
                        'icon' => 'el-icon-search'
                    ],
                ]
            ]
        ]);
        $raw = [];
        $fileType = request()->param('fileType');
        if ($fileType) $raw['suffix'] = $fileType;

        $searchOptions = $search->getGlobals()->format();

        Form::buildColumns(Form::buildForm($columns,$raw));


        return View::fetch('', [
            'search' => [
                'theme' => implode('', $search->theme()),
                'style' => implode('', $search->getStyle()),
                'script' => implode('', $search->getScript()),
                'options' => json_encode(count($searchOptions) > 0 ? $searchOptions : (object)[], JSON_UNESCAPED_UNICODE),
                'columns' => json_encode($search->getColumns(), JSON_UNESCAPED_UNICODE),
            ],
            'ext'    => (new Uploader())->getExt(),
            'max_count' =>  request()->get('max_count',0)
        ]);
    }


    public function upload(){
        try {
            if (input('base64')) {
                $file = input('img_base64_data');
            }else{
                $file = request()->file('file');
            }
            $fileType = input('fileType', 'image');
            $url = (new Uploader())->handle($file, [
                'uploader' => session('admin.id'),
            ], $fileType);
        } catch (\Exception $e){
            return _error($e->getMessage());
        }
        return _success('上传成功', ['url' => $url]);
    }

}